package at.ac.thornwishstalon.ocrapp.ocr;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import at.ac.thornwishstalon.ocrapp.R;
import at.ac.thornwishstalon.ocrapp.main.fragments.OCRAPPDialogHandler;
import at.ac.thornwishstalon.ocrapp.main.fragments.OcrappDialogFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

public class TesseractTask extends AsyncTask<TesseractTaskParamWrapper, Integer, TesseractResultWrapper> {

	private final String TAG="AsynchTesseractTask";
	//private OcrappDialogFragment dialog;
	private Context context;
	private RectF rect;
	private ProgressDialog dialog;

	private OCRAPPDialogHandler handler;

	public TesseractTask(Context context, ProgressDialog dialog){

		this.context=context;
		//this.pd=pd;
		this.context= context;
		this.handler=handler;
		this.dialog=dialog;
	}

	@Override
	protected void onPreExecute() {
		Log.d(TAG, "show dialog");
		//OCRAPPDialogHandler.getInstance().showDialog();
		if(!dialog.isShowing())
			dialog.show();
	}

	@Override
	protected TesseractResultWrapper doInBackground(TesseractTaskParamWrapper... params) {
		Log.d(TAG, "do in background");
		TesseractHelper	helper= TesseractHelper.getInstance();
		TesseractTaskParamWrapper param= params[0];

		Uri uriString=param.getImageUri();
		rect= param.getRect();


		if(uriString!=null){
			InputStream is;
			try {
				is = context.getContentResolver().openInputStream(uriString);

				Bitmap tempBitmap = BitmapFactory.decodeStream(is);
				is.close();

				helper.setImage(tempBitmap);

				if(rect!=null){
					Log.d(TAG, "rec: "+ rect.toString()+"RECT WAS SET!!!!!!!");
					helper.setRectangel(rect);				
				}

				String text=helper.getText();
				Log.i(TAG, "got text");
				ArrayList<Rect> charBB=helper.getWordRects();
				ArrayList<Rect> tl=helper.getTextLines();
				ArrayList<Rect> re=helper.getRegions();

				ArrayList<Rect> mappedBB = new ArrayList<Rect>();;
				ArrayList<Rect> mappedTL= new ArrayList<Rect>();;
				ArrayList<Rect> mappedRE= new ArrayList<Rect>();;


				if(rect !=null){
					int x= Math.round(rect.left);
					int y= Math.round(rect.top);
					Log.i(TAG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! x: "+x+" y:"+y);
					Rect tmp= new Rect();
					Matrix translate= translate();


					for(Rect r: charBB){

						mappedBB.add(apply(r, translate));

					}

					for(Rect rec: tl){

						//Log.d(TAG, "region from tessTASK"+ r.toString());
						//tmp.set(left,top,right, bottom);										
						mappedTL.add(apply(rec, translate));

					}
					for(Rect r: re){
						mappedRE.add(apply(r, translate));

					}


				}

				TesseractResultWrapper result;
				if(rect==null){
					result= new TesseractResultWrapper(charBB, tl, re, text);
				}
				else{
					result= new TesseractResultWrapper(mappedBB, mappedTL, mappedRE, text);
					//result= new TesseractResultWrapper(mappedBB, mappedTL, mappedRE, text);
				}

				return result;

			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return null;

			} catch (IOException e) {				
				e.printStackTrace();
				return null;
			}
		}
		else return null;
	}


	@Override
	protected void onPostExecute(TesseractResultWrapper result) {
		super.onPostExecute(result);	
		Log.d(TAG, "dismiss dialog");
		//OCRAPPDialogHandler.getInstance().dismissDialog();
		dialog.dismiss();
	}




	private Matrix translate(){
		Matrix trans= new Matrix();
		trans.setTranslate(rect.left, rect.top);
		return trans;
	}

	private Rect apply(Rect src, Matrix matrix){
		Rect dst = new Rect();
		float[] tmp = new float[]{src.left, src.bottom, src.right, src.top};
		matrix.mapPoints(tmp);
		dst.set(Math.round(tmp[0]), Math.round(tmp[3]), Math.round(tmp[2]), Math.round(tmp[1]));

		return dst;
	}







}
